package nina.yiyou.attendance.s10merge.compute.offgain

import nina.yiyou.attendance.s10merge.compute.AttendDetailMerger

import java.time.LocalDate
import java.time.LocalDateTime

class OffGainCalculator {

  interface NextDay {

    LocalDateTime latePoint()
  }

  OffGainCalculator(AttendDetailMerger.PunchDay day, NextDay nextDay) {
    _day = day
    _nextDay = nextDay
  }

  double calc() {
    LocalDate curDate = _day.date()
    LocalDateTime punchOut = _day.punchOut()
    if (!punchOut) {
      return 0
    }

    LocalDate nextDate = curDate.plusDays(1)
    def isOutAfter = { LocalDateTime t -> !punchOut.isBefore(t) }
    if (isOutAfter(nextDate.atTime(12, 00))) {
      return 7.5
    }
    if (isOutAfter(nextDate.atTime(7, 00))) {
      return 4.5
    }

    LocalDateTime nextShouldIn = _nextDay.latePoint()
    LocalDateTime pm1 = nextDate.atTime(13, 00)
    def workAm = { nextShouldIn ? nextShouldIn.isBefore(pm1) : true }

    if (isOutAfter(nextDate.atTime(3, 00))) {
      return workAm() ? 7.5 : 4.5
    }
    if (isOutAfter(nextDate.atTime(0, 00))) {
      return workAm() ? 3 : 0
    }

    if (isOutAfter(curDate.atTime(22, 00))) {
      // 暂时去除晚上10点的档位
    }
    return 0
  }

  private final AttendDetailMerger.PunchDay _day

  private final NextDay _nextDay
}
